Loading libraries

library(tidyverse)
library(gapminder)
library(here)

Section 3.4: Build your plots layer by laywer

Figure 3.5: Life expectancy vs GDP, using a GAM smoother

p <- ggplot(data = gapminder, mapping = aes(x = gdpPercap,y = lifeExp))
p + geom_smooth()
## `geom_smooth()` using method = 'gam' and formula 'y ~ s(x, bs = "cs")'

Figure 3.6: Life expectancy vs GDP, using a GAM smoother and points

p <- ggplot(data = gapminder, mapping = aes(x = gdpPercap,y = lifeExp))
p + geom_smooth() + geom_point()

Figure 3.7: Life expectancy vs GDP, using a linear model and points

p <- ggplot(data = gapminder, mapping = aes(x = gdpPercap,y = lifeExp))
p +  geom_point() + geom_smooth(method = "lm")

Figure 3.8: Life expectancy vs GDP scatterplot, with a GAM smoother and a log scale on the x-axis

p <- ggplot(data = gapminder, mapping = aes(x = gdpPercap,y = lifeExp))
p +  geom_point() + geom_smooth(method = "gam") + scale_x_log10()

Figure 3.9: Life expectancy vs GDP scatterplot, with a GAM smoother and a log scale on the x-axis, with x-labels as dollar values

p <- ggplot(data = gapminder, mapping = aes(x = gdpPercap,y = lifeExp))
p +  geom_point() + geom_smooth(method = "gam") + scale_x_log10(labels = scales::dollar)

ggplot(data = gapminder,
       mapping = aes(x = gdpPercap,y =lifeExp,color = continent)) + 
    geom_point() + scale_x_log10(labels = scales::dollar)

Figure 3.10: Same as above, but setting color independently of dataset variables.

p <- ggplot(data = gapminder,
            mapping = aes(x = gdpPercap, y = lifeExp,
            color = c("purple")))

p + geom_point() + geom_smooth(method = "loess") + scale_x_log10()

Figure 3.11: Setting the color attribute of the points directly

p <- ggplot(data = gapminder, mapping = aes(x = gdpPercap,
                                            y = lifeExp))

p + geom_point(color = "purple") + geom_smooth(method = "loess") + 
    scale_x_log10(labels = scales::dollar)

Figure 3.12: Changing other arguments

p <- ggplot(gapminder,aes(x = gdpPercap, y = lifeExp))
p + geom_point(alpha = 0.3) + geom_smooth(color = "orange", se = FALSE,
                                          size =1.5, method = "lm") + 
    scale_x_log10()

Figure 3.13: A more polished plt of Life Expectancy vs GDP

p <- ggplot(gapminder, aes(x = gdpPercap, y = lifeExp))
p + geom_point(alpha = 0.3) +  ###Setting transparency of points
    geom_smooth(method = "gam") + ##Setting method
    scale_x_log10(labels = scales::dollar) + 
    labs(x = "GDP Per Capita in U$",
         y = "Life Expectancy in Years",
         title = "Economic Growth and Life Expectancy",
         subtitle = "Data points are country years",
         caption = "Source: Gapminder")

Figure 3.14: Mapping the continent variable to the color aesthetic

p <- ggplot(data = gapminder,
            mapping = aes(x = gdpPercap,
                          y = lifeExp,
                          color = continent))
p + geom_point() + geom_smooth(method = "loess") + scale_x_log10() + 
    theme(legend.position = "top")

Figure 3.15: Mapping the continent variable to the color and fill aesthetics

p <- ggplot(gapminder, aes(x = gdpPercap,
                           y = lifeExp,
                           color = continent,
                           fill = continent))
p + geom_point() + geom_smooth(method = "loess") + scale_x_log10()

Figure 3.16: Mapping aesthetics on a per geom basis. Color is mapped to continent for the points, but not for the smoother.

p <- ggplot(data = gapminder, mapping = aes(x = gdpPercap,
                                            y = lifeExp))

p + geom_point(mapping = aes(color= continent)) + 
    geom_smooth(method = "loess") + 
    scale_x_log10()

Figure 3.17; Mapping a continuous variable to color

p <- ggplot(data = gapminder,
            mapping = aes(x = gdpPercap, y = lifeExp))
p + geom_point(mapping = aes(color = log(pop))) + scale_x_log10()

Figure 3.x1: Setting the size of output for each R markdown chunk

p <- ggplot(data = gapminder,
            mapping = aes(x = gdpPercap, y = lifeExp))
p + geom_point(mapping = aes(color = log(pop))) + scale_x_log10()